package ilarele.planner.Adapters;

import ilarele.planner.Activities.R;
import ilarele.planner.Project.Project;
import ilarele.planner.Project.Task;

import java.util.ArrayList;

import android.content.Context;
import android.content.res.TypedArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class GalleryTaskAdapter extends BaseAdapter
{
	private Context context;
	private int itemBackground;
	private Project project;
	private ArrayList<Task> content;
	private int layout_item_id;
	private LayoutInflater m_inflater;

	public GalleryTaskAdapter(Context c, int taskLayout, ArrayList<Task> arrayList,
			Project project2)
	{
		super();
		content = arrayList;
		project = project2;
		layout_item_id = taskLayout;
		context = c;

		m_inflater = LayoutInflater.from(context);
		// ---setting the style---
		TypedArray a = context.obtainStyledAttributes(R.styleable.Gallery1);
		itemBackground = a.getResourceId(
				R.styleable.Gallery1_android_galleryItemBackground, 0);
		a.recycle();
	}

	// ---returns the number of images---
	public int getCount()
	{
		return content.size();
	}

	public Task getItem(int position)
	{
		return content.get(position);
	}

	public long getItemId(int position)
	{
		return position;
	}

	// ---returns an ImageView view---
	public View getView(int position, View convertView, ViewGroup parent)
	{

		Task crt_task = (Task) getItem(position);
		if (convertView == null)
		{
			convertView = m_inflater.inflate(layout_item_id, parent, false);
		}

		// GUI
		TextView task_name = (TextView) convertView
				.findViewById(R.id.task_name);
		task_name.setText(crt_task.getName());
		ImageButton task_pic = (ImageButton) convertView
				.findViewById(R.id.task_pic);
		
		task_pic.setScaleType(ImageView.ScaleType.FIT_XY);
		convertView.setLayoutParams(new Gallery.LayoutParams(150, 120));
		convertView.setBackgroundResource(itemBackground);
		return convertView;
	}
}
